home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / modules / stores.js < prev    next >
Text File  |  2008-08-08  |  8KB  |  285 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Dan Mills <thunder@mozilla.com>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. const EXPORTED_SYMBOLS = ['Store',
  38.                           'SnapshotStore'];
  39.  
  40. const Cc = Components.classes;
  41. const Ci = Components.interfaces;
  42. const Cr = Components.results;
  43. const Cu = Components.utils;
  44.  
  45. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  46. Cu.import("resource://weave/log4moz.js");
  47. Cu.import("resource://weave/constants.js");
  48. Cu.import("resource://weave/util.js");
  49. Cu.import("resource://weave/async.js");
  50.  
  51. Function.prototype.async = Async.sugar;
  52.  
  53. /*
  54.  * Data Stores
  55.  * These can wrap, serialize items and apply commands
  56.  */
  57.  
  58. function Store() {
  59.   this._init();
  60. }
  61. Store.prototype = {
  62.   _logName: "Store",
  63.   _yieldDuringApply: true,
  64.  
  65.   // set this property in child object's wrap()!
  66.   _lookup: null,
  67.  
  68.   __os: null,
  69.   get _os() {
  70.     if (!this.__os)
  71.       this.__os = Cc["@mozilla.org/observer-service;1"]
  72.         .getService(Ci.nsIObserverService);
  73.     return this.__os;
  74.   },
  75.  
  76.   __json: null,
  77.   get _json() {
  78.     if (!this.__json)
  79.       this.__json = Cc["@mozilla.org/dom/json;1"].
  80.         createInstance(Ci.nsIJSON);
  81.     return this.__json;
  82.   },
  83.  
  84.   _init: function Store__init() {
  85.     this._log = Log4Moz.Service.getLogger("Service." + this._logName);
  86.   },
  87.  
  88.   applyCommands: function Store_applyCommands(commandList) {
  89.     let self = yield;
  90.  
  91.     for (var i = 0; i < commandList.length; i++) {
  92.       if (this._yieldDuringApply) {
  93.         Utils.makeTimerForCall(self.cb);
  94.         yield; // Yield to main loop
  95.       }
  96.       var command = commandList[i];
  97.       this._log.trace("Processing command: " + this._json.encode(command));
  98.       switch (command["action"]) {
  99.       case "create":
  100.         this._createCommand(command);
  101.         break;
  102.       case "remove":
  103.         this._removeCommand(command);
  104.         break;
  105.       case "edit":
  106.         this._editCommand(command);
  107.         break;
  108.       default:
  109.         this._log.error("unknown action in command: " + command["action"]);
  110.         break;
  111.       }
  112.     }
  113.     self.done();
  114.   },
  115.  
  116.   // override only if neccessary
  117.   _itemExists: function Store__itemExists(GUID) {
  118.     if (GUID in this._lookup)
  119.       return true;
  120.     else
  121.       return false;
  122.   },
  123.  
  124.   // override these in derived objects
  125.  
  126.   // wrap MUST save the wrapped store in the _lookup property!
  127.   wrap: function Store_wrap() {
  128.     throw "wrap needs to be subclassed";
  129.   },
  130.  
  131.   wipe: function Store_wipe() {
  132.     throw "wipe needs to be subclassed";
  133.   },
  134.  
  135.   _resetGUIDs: function Store__resetGUIDs() {
  136.     let self = yield;
  137.     // default to do nothing
  138.   },
  139.   resetGUIDs: function Store_resetGUIDs(onComplete) {
  140.     this._resetGUIDs.async(this, onComplete);
  141.   }
  142. };
  143.  
  144. function SnapshotStore(name) {
  145.   this._init(name);
  146. }
  147. SnapshotStore.prototype = {
  148.   _logName: "SnapStore",
  149.  
  150.   _filename: null,
  151.   get filename() {
  152.     if (this._filename === null)
  153.       throw "filename is null";
  154.     return this._filename;
  155.   },
  156.   set filename(value) {
  157.     this._filename = value + ".json";
  158.   },
  159.  
  160.   // Last synced tree, version, and GUID (to detect if the store has
  161.   // been completely replaced and invalidate the snapshot)
  162.  
  163.   _data: {},
  164.   get data() { return this._data; },
  165.   set data(value) { this._data = value; },
  166.  
  167.   _version: 0,
  168.   get version() { return this._version; },
  169.   set version(value) { this._version = value; },
  170.  
  171.   _GUID: null,
  172.   get GUID() {
  173.     if (!this._GUID)
  174.       this._GUID = Utils.makeGUID();
  175.     return this._GUID;
  176.   },
  177.   set GUID(GUID) {
  178.     this._GUID = GUID;
  179.   },
  180.  
  181.   _init: function SStore__init(name) {
  182.     this.filename = name;
  183.     this._log = Log4Moz.Service.getLogger("Service." + this._logName);
  184.   },
  185.  
  186.   _createCommand: function SStore__createCommand(command) {
  187.     this._data[command.GUID] = Utils.deepCopy(command.data);
  188.   },
  189.  
  190.   _removeCommand: function SStore__removeCommand(command) {
  191.     delete this._data[command.GUID];
  192.   },
  193.  
  194.   _editCommand: function SStore__editCommand(command) {
  195.     if ("GUID" in command.data) {
  196.       // special-case guid changes
  197.       let newGUID = command.data.GUID,
  198.           oldGUID = command.GUID;
  199.  
  200.       this._data[newGUID] = this._data[oldGUID];
  201.       delete this._data[oldGUID];
  202.  
  203.       for (let GUID in this._data) {
  204.         if (("parentGUID" in this._data[GUID]) &&
  205.             (this._data[GUID].parentGUID == oldGUID))
  206.           this._data[GUID].parentGUID = newGUID;
  207.       }
  208.     }
  209.     for (let prop in command.data) {
  210.       if (prop == "GUID")
  211.         continue;
  212.       if (command.GUID in this._data)
  213.         this._data[command.GUID][prop] = command.data[prop];
  214.       else
  215.         this._log.warn("Warning! Edit command for unknown item: " + command.GUID);
  216.     }
  217.   },
  218.  
  219.   save: function SStore_save() {
  220.     this._log.info("Saving snapshot to disk");
  221.  
  222.     let file = Utils.getProfileFile(
  223.       {path: "weave/snapshots/" + this.filename,
  224.        autoCreate: true}
  225.       );
  226.  
  227.     let out = {version: this.version,
  228.                GUID: this.GUID,
  229.                snapshot: this.data};
  230.     out = this._json.encode(out);
  231.  
  232.     let [fos] = Utils.open(file, ">");
  233.     fos.writeString(out);
  234.     fos.close();
  235.   },
  236.  
  237.   load: function SStore_load() {
  238.     let file = Utils.getProfileFile("weave/snapshots/" + this.filename);
  239.     if (!file.exists())
  240.       return;
  241.  
  242.     try {
  243.       let [is] = Utils.open(file, "<");
  244.       let json = Utils.readStream(is);
  245.       is.close();
  246.       json = this._json.decode(json);
  247.  
  248.       if (json && 'snapshot' in json && 'version' in json && 'GUID' in json) {
  249.         this._log.debug("Read saved snapshot from disk");
  250.         this.data = json.snapshot;
  251.         this.version = json.version;
  252.         this.GUID = json.GUID;
  253.       }
  254.     } catch (e) {
  255.       this._log.warn("Could not parse saved snapshot; reverting to initial-sync state");
  256.       this._log.debug("Exception: " + e);
  257.     }
  258.   },
  259.  
  260.   serialize: function SStore_serialize() {
  261.     let json = this._json.encode(this.data);
  262.     json = json.replace(/:{type/g, ":\n\t{type");
  263.     json = json.replace(/}, /g, "},\n  ");
  264.     json = json.replace(/, parentGUID/g, ",\n\t parentGUID");
  265.     json = json.replace(/, index/g, ",\n\t index");
  266.     json = json.replace(/, title/g, ",\n\t title");
  267.     json = json.replace(/, URI/g, ",\n\t URI");
  268.     json = json.replace(/, tags/g, ",\n\t tags");
  269.     json = json.replace(/, keyword/g, ",\n\t keyword");
  270.     return json;
  271.   },
  272.  
  273.   wrap: function SStore_wrap() {
  274.     return this.data;
  275.   },
  276.  
  277.   wipe: function SStore_wipe() {
  278.     this.data = {};
  279.     this.version = -1;
  280.     this.GUID = null;
  281.     this.save();
  282.   }
  283. };
  284. SnapshotStore.prototype.__proto__ = new Store();
  285.